import axios from 'axios';
import base64 from '../assets/js/base64'
import { Message, Loading } from 'element-ui'
let loadingInstance = null //这里是loading

const service = axios.create({
    // process.env.NODE_ENV === 'development' 来判断是否开发环境
    baseURL: 'http://121.43.162.250:18037',
    // baseURL:'http://192.168.10.121:8080',
    timeout: 100000,
    headers: {
        'Content-Type': 'application/json;charset=UTF-8'
    }
});
// 添加请求拦截器
service.interceptors.request.use(
    config => {
        loadingInstance = Loading.service({
            lock: true,
            text: '加载中...'
        })
        return config
    },
    error => {
        console.log(error);
        return Promise.reject();
    }
)
// 添加响应拦截器
service.interceptors.response.use(
    response => {
        loadingInstance.close()
        // return response.data
        console.log(response)
        if (response.status === 200) {
            return response.data;
        } else {
            Promise.reject();
        }
    },
    error => {
        loadingInstance.close()
        if (error && error.response) {
            switch (error.response.status) {
                case 400:
                    Message({
                        message:'错误请求',
                        type:'error',
                        duration:3*1000
                    })
                    error.message = '错误请求';
                    break;
                case 401:
                    Message({
                        message:'未授权，请重新登录',
                        type:'error',
                        duration:3*1000
                    })
                    error.message = '未授权，请重新登录';
                    getToken();
                    break;
                case 403:
                    Message({
                        message:'拒绝访问',
                        type:'error',
                        duration:3*1000
                    })
                    error.message = '拒绝访问';
                    break;
                case 404:
                    Message({
                        message:'请求错误,未找到该资源',
                        type:'error',
                        duration:3*1000
                    })
                    error.message = '请求错误,未找到该资源';
                    break;
                case 405:
                    Message({
                        message:'请求方法未允许',
                        type:'error',
                        duration:3*1000
                    })
                    error.message = '请求方法未允许';
                    break;
                case 408:
                    Message({
                        message:'请求超时',
                        type:'error',
                        duration:3*1000
                    })
                    error.message = '请求超时';
                    break;
                case 500:
                    Message({
                        message:'服务器端出错',
                        type:'error',
                        duration:3*1000
                    })
                    error.message = '服务器端出错';
                    break;
                case 501:
                    Message({
                        message:'网络未实现',
                        type:'error',
                        duration:3*1000
                    })
                    error.message = '网络未实现';
                    break;
                case 502:
                    Message({
                        message:'网络错误',
                        type:'error',
                        duration:3*1000
                    })
                    error.message = '网络错误';
                    break;
                case 503:
                    Message({
                        message:'服务不可用',
                        type:'error',
                        duration:3*1000
                    })
                    error.message = '服务不可用';
                    break;
                case 504:
                    Message({
                        message:'网络超时',
                        type:'error',
                        duration:3*1000
                    })
                    error.message = '网络超时';
                    break;
                case 505:
                    Message({
                        message:'http版本不支持该请求',
                        type:'error',
                        duration:3*1000
                    })
                    error.message = 'http版本不支持该请求';
                    break;
                default:
                    Message({
                        message:`未知错误${error.response.status}`,
                        type:'error',
                        duration:3*1000
                    })
                    error.message = `未知错误${error.response.status}`;
            }
        } else {
            Message({
                message:'连接到服务器失败',
                type:'error',
                duration:3*1000
            })
            getToken();
            error.message = "连接到服务器失败";
        }

        return Promise.reject(error);
    }
);
// getToken
function getToken(){
    let secret = base64._utf8_decode("faceAdmin:VF4e5u9wbLMYOMn6Sun3AyUncBmEf8");
    let secretmd5 = base64.encode(secret);
    return new Promise((resolve, reject) =>{
        service({
            method:'get',
            url:'https://service.safedove.cn/administrator/getToken?userName=zl&passWord=admin',
            header: {
                'content': secretmd5 // 默认值
            }
        }).then(res=>{
            console.log(res)
            resolve(res);
        }).catch(err=>{
            console.log(err)
            reject(err);
        })
    })
}
// get方法
export function get(url, params){
    return new Promise((resolve, reject) =>{
        service({
            method:'get',
            url:url,
            params: params
        }).then(res => {
            resolve(res);
        }).catch(err =>{
            reject(err)
        })
    });
}
// post方法
export function post(url, params) {
    return new Promise((resolve, reject) =>{
        service({
            method:'post',
            url:url,
            params: params
        }).then(res => {
            resolve(res);
        }).catch(err =>{
            reject(err)
        })
    });
}
// upload方法
export function upload(url, params) {
    return new Promise((resolve, reject) =>{
        service({
            method:'post',
            url:url,
            params: params,
            headers: { 'Content-Type': 'multipart/form-data' }
        }).then(res => {
            resolve(res);
        }).catch(err =>{
            reject(err)
        })
    });
}
// 登录
export const logintest=params=>post('/logintest',params);
